package com.lc.pianostudent.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.adapter.ShopAdapter;
import com.lc.pianostudent.adapter.ShopHomeAdapter;
import com.lc.pianostudent.bean.AccessBean;
import com.lc.pianostudent.bean.ShopBean;
import com.lc.pianostudent.bean.ShophomeBean;
import com.lc.pianostudent.conn.PostGoodsCheck;
import com.lc.pianostudent.conn.PostGoodsIndex;
import com.lc.pianostudent.conn.PostGoodsShopping;
import com.lc.pianostudent.conn.PostGoodsTypechange;
import com.lc.pianostudent.conn.PostHomeCheck;
import com.lc.pianostudent.conn.PostHomeIndex;
import com.lc.pianostudent.conn.PostHomePreser;
import com.lc.pianostudent.conn.PostHomeTypechange;
import com.lc.pianostudent.dialog.AskDialog;
import com.lc.pianostudent.dialog.FinishDialog;
import com.lc.pianostudent.dialog.ShopDialog;
import com.lc.pianostudent.util.DensityUtil;
import com.squareup.picasso.Picasso;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static com.lc.pianostudent.BaseApplication.BasePreferences;
import static com.lc.pianostudent.BaseApplication.getContext;

/**
 * Created by Administrator on 2018/7/5.
 */

public class MyHomeActivity extends BaseActivity implements View.OnClickListener{

    private List<ShophomeBean> shopList = new ArrayList<>();
    @BoundView(R.id.shop_recyclerView) private RecyclerView recyclerView;
    private ShopHomeAdapter shopHomeAdapter;


    @BoundView(value = R.id.shop_save,isClick = true) private ImageView shop_save;

    private Timer timer = null;//计时器
    private TimerTask timerTask = null;
    private int i = 0;
    private FinishDialog dialog;

    @BoundView(R.id.hair) private ImageView hair;
    @BoundView(R.id.shoe) private ImageView shoe;
    @BoundView(R.id.jacket) private ImageView jacket;
    @BoundView(R.id.access) private ImageView access;
    @BoundView(R.id.suit) private ImageView suit;
    @BoundView(R.id.download) private ImageView download;
    @BoundView(R.id.maozi) private ImageView maozi;
    @BoundView(R.id.shoutao) private ImageView shoutao;
    @BoundView(R.id.wazi) private ImageView wazi;
    @BoundView(R.id.xianglian) private ImageView xianglian;
    @BoundView(R.id.erhuan) private ImageView erhuan;
    @BoundView(R.id.yanjing) private ImageView yanjing;
    @BoundView(R.id.mianshi) private ImageView mianshi;
    @BoundView(R.id.biaoqing) private ImageView biaoqing;
    @BoundView(R.id.huanzhuang_layout) private RelativeLayout huanzhuang_layout;
    private PostHomeIndex.PostHomeIndexInfo currentInfo;
    private PostHomeTypechange.PostHomeTypechangeInfo currentchangeInfo;
    private PostHomeCheck.PostHomeCheckInfo currentcheckInfo;

    private int tab_position;
    @BoundView(R.id.huangguan1)
    private ImageView huangguan1;
    @BoundView(R.id.huangguan2)
    private ImageView huangguan2;
    @BoundView(R.id.huangguan3)
    private ImageView huangguan3;
    @BoundView(R.id.tab_see_tegong)
    private TabLayout tabLayout;
    public List<AccessBean> accessShowshopList = new ArrayList<>();
    @BoundView(R.id.luomo) private ImageView luomo;


    //我的家首页
    public PostHomeIndex postHomeIndex = new PostHomeIndex(new AsyCallBack<PostHomeIndex.PostHomeIndexInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostHomeIndex.PostHomeIndexInfo postHomeIndexInfo) throws Exception {

            currentInfo = postHomeIndexInfo;
            for(int i = 0;i<postHomeIndexInfo.shopIndexList.size();i++){
                tabLayout.addTab(tabLayout.newTab().setText(postHomeIndexInfo.shopIndexList.get(i).getTitle()));
            }
            post();
            tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

//            Log.e("====sex",postHomeIndexInfo.sex+"---");
//            if(postHomeIndexInfo.sex.equals("2")){
//                huanzhuang_layout.setBackgroundResource(R.mipmap.shenti_nv);
//            }else {
//                huanzhuang_layout.setBackgroundResource(R.mipmap.shenti_nan);
//
//            }
            Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.mote).into(luomo);

//            Log.e("----",postHomeIndexInfo.huangguan+"====");
            //皇冠数
            if(postHomeIndexInfo.huangguan.equals("0")){
                huangguan1.setVisibility(View.INVISIBLE);
                huangguan2.setVisibility(View.INVISIBLE);
                huangguan3.setVisibility(View.INVISIBLE);
            }else if(postHomeIndexInfo.huangguan.equals("1")){
                huangguan1.setVisibility(View.VISIBLE);
                huangguan2.setVisibility(View.INVISIBLE);
                huangguan3.setVisibility(View.INVISIBLE);
            }else if(postHomeIndexInfo.huangguan.equals("2")){
                huangguan1.setVisibility(View.VISIBLE);
                huangguan2.setVisibility(View.VISIBLE);
                huangguan3.setVisibility(View.INVISIBLE);
            }else if(postHomeIndexInfo.huangguan.equals("3")){
                huangguan1.setVisibility(View.VISIBLE);
                huangguan2.setVisibility(View.VISIBLE);
                huangguan3.setVisibility(View.VISIBLE);
            }

            //头发
            if(postHomeIndexInfo.hair_id.equals("")){
                hair.setVisibility(View.INVISIBLE);
            }else {
                hair.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsIndexInfo.hair_picurl,hair);
                Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.hair_picurl).into(hair);
            }
            //上身
            if(postHomeIndexInfo.jack_id.equals("")){
                jacket.setVisibility(View.INVISIBLE);
            }else {
                jacket.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsIndexInfo.jack_picurl,jacket);
                Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.jack_picurl).into(jacket);
            }
            //下装
            if(postHomeIndexInfo.download_id.equals("")){
                download.setVisibility(View.INVISIBLE);
            }else {
                download.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsIndexInfo.download_picurl,download);
                Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.download_picurl).into(download);

            }

            //表情
            if(postHomeIndexInfo.expression_id.equals("")){
                biaoqing.setVisibility(View.INVISIBLE);
            }else {
                biaoqing.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsIndexInfo.download_picurl,download);
                Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.expression_picurl).into(biaoqing);

            }

            //配饰
            if(postHomeIndexInfo.accessShowList.size() == 0){
                access.setVisibility(View.INVISIBLE);
                maozi.setVisibility(View.INVISIBLE);
                erhuan.setVisibility(View.INVISIBLE);
                wazi.setVisibility(View.INVISIBLE);
                yanjing.setVisibility(View.INVISIBLE);
                shoutao.setVisibility(View.INVISIBLE);
                mianshi.setVisibility(View.INVISIBLE);
                xianglian.setVisibility(View.INVISIBLE);
            }else {

                for(int i = 0;i<postHomeIndexInfo.accessShowList.size();i++){
                    if(postHomeIndexInfo.accessShowList.get(i).getType().equals("14")){  //手套
                        shoutao.setVisibility(View.VISIBLE);

//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),shoutao);
                        Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.accessShowList.get(i).getPicurl()).into(shoutao);

                    }else
                    if(postHomeIndexInfo.accessShowList.get(i).getType().equals("13")){  //包
                        access.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsCheckInfo.accessShowList.get(i).getPicurl(),access);
                        Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.accessShowList.get(i).getPicurl()).into(access);
                    }else
                    if(postHomeIndexInfo.accessShowList.get(i).getType().equals("12")){  //耳环
                        erhuan.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),erhuan);
                        Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.accessShowList.get(i).getPicurl()).into(erhuan);

                    }else

                    if(postHomeIndexInfo.accessShowList.get(i).getType().equals("16")){  //袜子
                        wazi.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),wazi);
                        Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.accessShowList.get(i).getPicurl()).into(wazi);

                    }else
                    if(postHomeIndexInfo.accessShowList.get(i).getType().equals("18")){  //眼睛
                        yanjing.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),yanjing);
                        Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.accessShowList.get(i).getPicurl()).into(yanjing);

                    }else
                    if(postHomeIndexInfo.accessShowList.get(i).getType().equals("19")){  //帽子
                        maozi.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),maozi);
                        Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.accessShowList.get(i).getPicurl()).into(maozi);

                    }else
                    if(postHomeIndexInfo.accessShowList.get(i).getType().equals("20")){  //面饰
                        mianshi.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),mianshi);
                        Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.accessShowList.get(i).getPicurl()).into(mianshi);

                    } else
                    if(postHomeIndexInfo.accessShowList.get(i).getType().equals("21")){  //项链
                        xianglian.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),xianglian);
                        Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.accessShowList.get(i).getPicurl()).into(xianglian);

                    }
                }

            }
            //鞋子
            if(postHomeIndexInfo.shose_id.equals("")){
                shoe.setVisibility(View.INVISIBLE);
            }else {
                shoe.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsIndexInfo.shose_picurl,shoe);
                Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.shose_picurl).into(shoe);

            }
            //套装
            if(postHomeIndexInfo.suit_id.equals("")){
                suit.setVisibility(View.INVISIBLE);
            }else {
                suit.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsIndexInfo.suit_picurl,suit);
                Picasso.with(MyHomeActivity.this).load(postHomeIndexInfo.suit_picurl).into(suit);
            }


//            if(type == 0){
//                shopList.clear();
//            }
//            shopList.addAll(postHomeIndexInfo.homeList);
//            shopHomeAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });

    //我的家分类
    public PostHomeTypechange postHomeTypechange = new PostHomeTypechange(new AsyCallBack<PostHomeTypechange.PostHomeTypechangeInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostHomeTypechange.PostHomeTypechangeInfo postHomeTypechangeInfo) throws Exception {

            currentchangeInfo = postHomeTypechangeInfo;
            if(type == 0){
                shopList.clear();
            }
            shopList.addAll(postHomeTypechangeInfo.homeGoodsList);
            shopHomeAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });

    //商城显示
    public PostHomeCheck postHomeCheck = new PostHomeCheck(new AsyCallBack<PostHomeCheck.PostHomeCheckInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostHomeCheck.PostHomeCheckInfo postHomeCheckInfo) throws Exception {
            currentcheckInfo = postHomeCheckInfo;
            accessShowshopList.clear();
            accessShowshopList.addAll(postHomeCheckInfo.accessShowList);

            //头发
            if(postHomeCheckInfo.hair_id.equals("")){
                hair.setVisibility(View.INVISIBLE);
            }else {
                hair.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.hair_picurl,hair,R.mipmap.new_head);
                Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.hair_picurl).into(hair);
            }
            //上身
            if(postHomeCheckInfo.jack_id.equals("")){
                jacket.setVisibility(View.INVISIBLE);
            }else {
                jacket.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.jack_picurl,jacket);
                Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.jack_picurl).into(jacket);
            }
            //下装
            if(postHomeCheckInfo.download_id.equals("")){
                download.setVisibility(View.INVISIBLE);
            }else {
                download.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.download_picurl,download);
                Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.download_picurl).into(download);

            }

            //表情
            if(postHomeCheckInfo.expression_id.equals("")){
                biaoqing.setVisibility(View.INVISIBLE);
            }else {
                biaoqing.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsIndexInfo.download_picurl,download);
                Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.expression_picurl).into(biaoqing);

            }
            //配饰
            if(postHomeCheckInfo.accessShowList.size() == 0){
                access.setVisibility(View.INVISIBLE);
                maozi.setVisibility(View.INVISIBLE);
                erhuan.setVisibility(View.INVISIBLE);
                wazi.setVisibility(View.INVISIBLE);
                yanjing.setVisibility(View.INVISIBLE);
                shoutao.setVisibility(View.INVISIBLE);
                mianshi.setVisibility(View.INVISIBLE);
                xianglian.setVisibility(View.INVISIBLE);
            }else {
                for(int i = 0;i<postHomeCheckInfo.accessShowList.size();i++){


                    if(postHomeCheckInfo.accessShowList.get(i).getType().equals("14")){  //手套
                        shoutao.setVisibility(View.VISIBLE);

//                        GlideLoader.getInstance().get(postGoodsCheckInfo.accessShowList.get(i).getPicurl(),shoutao);
                        Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.accessShowList.get(i).getPicurl()).into(shoutao);

                    }else
                    if(postHomeCheckInfo.accessShowList.get(i).getType().equals("13")){  //包
                        access.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsCheckInfo.accessShowList.get(i).getPicurl(),access);
//                        Picasso.with(context).load(postGoodsCheckInfo.accessShowList.get(i).getPicurl()).into(access);
                        Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.accessShowList.get(i).getPicurl()).into(access);

                    }else
                    if(postHomeCheckInfo.accessShowList.get(i).getType().equals("12")){  //耳环
                        erhuan.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsCheckInfo.accessShowList.get(i).getPicurl(),erhuan);
                        Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.accessShowList.get(i).getPicurl()).into(erhuan);

                    }else

                    if(postHomeCheckInfo.accessShowList.get(i).getType().equals("16")){  //袜子
                        wazi.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsCheckInfo.accessShowList.get(i).getPicurl(),wazi);
                        Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.accessShowList.get(i).getPicurl()).into(wazi);

                    }else
                    if(postHomeCheckInfo.accessShowList.get(i).getType().equals("18")){  //眼睛
                        yanjing.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsCheckInfo.accessShowList.get(i).getPicurl(),yanjing);
                        Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.accessShowList.get(i).getPicurl()).into(yanjing);

                    }else
                    if(postHomeCheckInfo.accessShowList.get(i).getType().equals("19")){  //帽子
                        maozi.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsCheckInfo.accessShowList.get(i).getPicurl(),maozi);
                        Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.accessShowList.get(i).getPicurl()).into(maozi);

                    }else
                    if(postHomeCheckInfo.accessShowList.get(i).getType().equals("20")){  //面饰
                        mianshi.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsCheckInfo.accessShowList.get(i).getPicurl(),mianshi);
                        Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.accessShowList.get(i).getPicurl()).into(mianshi);

                    } else
                    if(postHomeCheckInfo.accessShowList.get(i).getType().equals("21")){  //项链
                        xianglian.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsCheckInfo.accessShowList.get(i).getPicurl(),xianglian);
                        Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.accessShowList.get(i).getPicurl()).into(xianglian);

                    }
                }

            }
            //鞋子
            if(postHomeCheckInfo.shose_id.equals("")){
                shoe.setVisibility(View.INVISIBLE);
            }else {
                shoe.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.shose_picurl,shoe);
                Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.shose_picurl).into(shoe);

            }
            //套装
            if(postHomeCheckInfo.suit_id.equals("")){
                suit.setVisibility(View.INVISIBLE);
            }else {
                suit.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.suit_picurl,suit);
                Picasso.with(MyHomeActivity.this).load(postHomeCheckInfo.suit_picurl).into(suit);

            }
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });

    //换装完毕
    public PostHomePreser postHomePreser = new PostHomePreser(new AsyCallBack<PostHomePreser.PostHomePreserInfo>(){


        @Override
        public void onSuccess(String toast, int type, Object o, PostHomePreser.PostHomePreserInfo postHomePreserInfo) throws Exception {

            i = 1;

            dialog.show();
            startTime();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        setBackTrue();

        initView();
    }

    private void initView(){
        setTitleName("我的家");
        dialog = new FinishDialog(this, R.layout.dialog_finish){

        };

        postHomeIndex.user_id = BaseApplication.BasePreferences.readUID();
        postHomeIndex.execute();

        LinearLayoutManager layoutManager=new LinearLayoutManager(MyHomeActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        shopHomeAdapter = new ShopHomeAdapter(this, shopList);
        recyclerView.setAdapter(shopHomeAdapter);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab_position = tab.getPosition();
                postHomeTypechange.user_id = BasePreferences.readUID();
                postHomeTypechange.type = currentInfo.shopIndexList.get(tab.getPosition()).getId();
                postHomeTypechange.execute(MyHomeActivity.this,false,0);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
//                Log.e("====","onTabUnselected"+tab.getPosition());
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                tab_position = tab.getPosition();
                postHomeTypechange.user_id = BaseApplication.BasePreferences.readUID();
                postHomeTypechange.type = currentInfo.shopIndexList.get(tab.getPosition()).getId();
                postHomeTypechange.execute(MyHomeActivity.this,false,0);
            }
        });

        shopHomeAdapter.setOnItemClickListener(new ShopHomeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.e("====jy","----");
                access.setVisibility(View.INVISIBLE);
                maozi.setVisibility(View.INVISIBLE);
                erhuan.setVisibility(View.INVISIBLE);
                wazi.setVisibility(View.INVISIBLE);
                yanjing.setVisibility(View.INVISIBLE);
                shoutao.setVisibility(View.INVISIBLE);
                mianshi.setVisibility(View.INVISIBLE);
                xianglian.setVisibility(View.INVISIBLE);
                postHomeCheck.user_id = BaseApplication.BasePreferences.readUID();
                postHomeCheck.type = currentchangeInfo.homeGoodsList.get(position).getTypeid();
                postHomeCheck.id = currentchangeInfo.homeGoodsList.get(position).getId();
                postHomeCheck.execute();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.shop_save:

                postHomePreser.user_id = BaseApplication.BasePreferences.readUID();
                postHomePreser.execute(MyHomeActivity.this,false,0);
            case R.id.rl_title_back:
                stopTime();
                dialog.dismiss();

                break;
        }
    }




    private Handler mHandler = new Handler(){
        public void handleMessage(Message msg) {


//            Log.e("--i",i+"===jy");
            if(i==0){
                stopTime();
                dialog.dismiss();

            }else {
                startTime();
            }
        };
    };

    /**
     * 开始自动减时
     */
    private void startTime() {
        if(timer==null){
            timer = new Timer();
        }

        timerTask = new TimerTask() {

            @Override
            public void run() {
                i--;//自动减1
                Message message = Message.obtain();
                message.arg1=i;
                mHandler.sendMessage(message);//发送消息
            }
        };
        if(timer != null){
            timer.schedule(timerTask, 1000);//1000ms执行一次
        }

    }
    /**
     * 停止自动减时
     */
    private void stopTime() {
        if(timer!=null){
            timer.purge();
            timer = null;
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        stopTime();
        dialog.dismiss();

    }

    private void post(){

        try {
            //拿到tabLayout的mTabStrip属性
            Field mTabStripField = tabLayout.getClass().getDeclaredField("mTabStrip");
            mTabStripField.setAccessible(true);

            LinearLayout mTabStrip = (LinearLayout) mTabStripField.get(tabLayout);

            int dp10 = DensityUtil.dp2px(getContext(), 10);

            for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                View tabView = mTabStrip.getChildAt(i);

                //拿到tabView的mTextView属性
                Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                mTextViewField.setAccessible(true);

                TextView mTextView = (TextView) mTextViewField.get(tabView);

                tabView.setPadding(0, 0, 0, 0);

                //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                int width = 0;
                width = mTextView.getWidth();
                if (width == 0) {
                    mTextView.measure(0, 0);
                    width = mTextView.getMeasuredWidth();
                }

                //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                params.width = width ;
                params.leftMargin = dp10;
                params.rightMargin = dp10;
                tabView.setLayoutParams(params);

                tabView.invalidate();
            }

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    };

}
